package com.example.hasee.zhcd.Application;

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageManager;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.widget.Toast;

import com.example.hasee.zhcd.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.PushAgent;
import com.umeng.message.UmengNotificationClickHandler;
import com.umeng.message.entity.UMessage;

import java.util.Map;

public class App extends Application {
    private String token;
    //手机基本数据参数
    public static String appVerson = "";//App版本号
    public static int mScreenWidth; //屏幕宽度
    public static int mScreenHeight; //屏幕高度
    public static String PHONEID = ""; //手机ID
    public static String sysVersion = "Android " + android.os.Build.VERSION.RELEASE;
    public static String phoneMode = android.os.Build.MODEL;


    public static String updateVersion = "";
    public static String updateUrl = "";
    public static String updateContent = "";


    @Override
    public void onCreate() {
        super.onCreate();
        initData(); //开始手机数据初始化
        DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder() //
                .showImageForEmptyUri(R.mipmap.ic_launcher) //
                .showImageOnFail(R.mipmap.ic_launcher) //
                .cacheInMemory(true) //
                .cacheOnDisk(true) //
                .build();//
        ImageLoaderConfiguration config = new ImageLoaderConfiguration//
                .Builder(getApplicationContext())//
                .defaultDisplayImageOptions(defaultOptions)//
                .discCacheSize(50 * 1024 * 1024)//
                .discCacheFileCount(100)// 缓存一百张图片
                .writeDebugLogs()//
                .build();//
        ImageLoader.getInstance().init(config);
    }

    //手机基本数据初始化
    private void initData() {
        /**
         * 获取手机屏幕尺寸
         * */
        mScreenWidth = this.getApplicationContext().getResources()
                .getDisplayMetrics().widthPixels;
        mScreenHeight = this.getApplicationContext().getResources()
                .getDisplayMetrics().heightPixels;
        /**
         * 获取手机唯一标示
         * */
        try {
            TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
            PHONEID = tm.getDeviceId();
        } catch (Exception e) {
            PHONEID = Settings.System.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
        }

        /**
         * app版本信息获取
         * */
        try {
            appVerson = getPackageManager().getPackageInfo(this.getPackageName(), 0).versionName;
            updateVersion = appVerson;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        PushAgent mPushAgent = PushAgent.getInstance(this);
//注册推送服务，每次调用register方法都会回调该接口
        mPushAgent.register(new IUmengRegisterCallback() {

            @Override
            public void onSuccess(String deviceToken) {
                //注册成功会返回device token
                token = deviceToken;
            }

            @Override
            public void onFailure(String s, String s1) {

            }
        });
        UmengNotificationClickHandler notificationClickHandler = new UmengNotificationClickHandler() {
            @Override
            public void dealWithCustomAction(Context context, UMessage msg) {
                Toast.makeText(context, "haha", Toast.LENGTH_LONG).show();
            }
        };
        mPushAgent.setNotificationClickHandler(notificationClickHandler);
    }


    // 用于存放倒计时时间
    public static Map<String, Long> map;
}
